package com.tools.scanner.ui.pdf

import android.net.Uri
import com.itextpdf.text.Document
import com.itextpdf.text.Element
import com.itextpdf.text.Image
import com.itextpdf.text.PageSize
import com.itextpdf.text.Rectangle
import com.itextpdf.text.pdf.PdfWriter
import java.io.File
import java.io.FileOutputStream
import java.net.URL

class PdfUtil {

    public fun imgTransformPdf(file: File, imageList: List<Uri>): Boolean {
        val doc = Document(PageSize.A4, 0f, 0f, 0f, 0f);
        try {
            PdfWriter.getInstance(doc, FileOutputStream(file))
            doc.open();
            for (item in imageList) {
                val img = Image.getInstance(URL(item.toString()));
                img.alignment = Element.ALIGN_CENTER
                img.scaleToFit(Rectangle(PageSize.A4))
                doc.add(img)
                doc.newPage()
            }

            return true
        } catch (e: Exception) {
            e.printStackTrace();
            return false
        } finally {
            doc.close()
        }
    }

}